/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dameki.core;

import dameki.game.GameBoard;
import dameki.game.GameMove;
import dameki.game.player.IPlayer;
import dameki.game.Stone;
import dameki.output.IGUI;

/**
 *
 * @author Tobi
 */
public class Controller {

    private GameBoard board;
    private boolean playing;
    private IPlayer player1;
    private IPlayer player2;
    private IGUI[] gui;

    public Controller(IPlayer p1, IPlayer p2, IGUI... gui) {
        board = new GameBoard();
        //init the players
        this.player1 = p1;
        player1.setController(this);
        player1.setStoneList(board.getStoneList(player1.getColor()));

        this.player2 = p2;
        player2.setController(this);
        player2.setStoneList(board.getStoneList(player2.getColor()));

        this.gui = gui;
    }

    public void playGame() 
    {
        playing = true;
        
        for(int j=0; j<gui.length; j++)
        {
            gui[j].update(board.getField());
        }
        
        while (playing) {
            System.out.println("Player "+player1.getColorString()+":");
            if(!board.getStoneList(player1.getColor()).isEmpty())
            {
                callPlayer(player1);
            }
            else
            {
                System.out.println("Player " + player1.getColorString() + " lost the game. No Stones left.");
                playing = false;
                return;                
            }
            if (playing) 
            {
                System.out.println("Player "+player2.getColorString()+":");
                if(!board.getStoneList(player2.getColor()).isEmpty())
                {
                    callPlayer(player2);
                }
                else
                {
                    System.out.println("Player " + player2.getColorString() + " lost the game. No Stones left.");
                    playing = false;
                    return;
                }
            }

//            calcMove(new GameMove(board.getField()[2][1], 3, 2), player1);
//            //update GUI
//            gui.update(board.getField());
//            calcMove(new GameMove(board.getField()[5][2], 4, 3), player2);
//            gui.update(board.getField());
//            calcMove(new GameMove(board.getField()[4][3], 2, 1), player2);
//            gui.update(board.getField());
            //playing = false;
        }
    }

    private void callPlayer(IPlayer player) {
        GameMove[] movements = player.move(copyfield(board.getField()));
        GameMove[] movements2 = null;
        if(movements!=null)
        {
            if(movements.length>0)
            {
                movements2 = new GameMove[movements.length];
                for(int i=0; i<movements.length; i++)
                {
                    Stone s = new Stone(movements[i].getStone().getColor());
                    s.setPosX(movements[i].getStone().getPosX());
                    s.setPosY(movements[i].getStone().getPosY());

                    movements2[i] = new GameMove(s, movements[i].getPosY(), movements[i].getPosX());
                }
            }
        }
        if (movements == null) 
        {
            System.out.println("Player " + player.getColorString() + " lost the game.");
            playing = false;
            return;
        }
        for (int i = 0; i < movements.length; i++) {
            if (!calcMove(movements[i], player)) {
                System.out.println("Wrong move from Player '" + player.getColorString() + "'");
            }
        }
        Stone[][] tempfield = copyfield(board.getField());
        for(int j=0; j<gui.length; j++)
        {
            gui[j].animate(movements2);
            gui[j].update(tempfield);
        }
    }

    private boolean calcMove(GameMove move, IPlayer player) {
        if (move != null && player != null && move.getStone() != null) {
            if (board.checkMove(move, player.getColor())) {
                board.move(move);
                return true;
            }
        }
        return false;
    }

    /**
     * Check if the move is valid
     *
     * @param s
     * @param pos
     * @param player
     * @return
     */
    public boolean checkMove(GameMove move, IPlayer player) {
        return board.checkMove(move, player.getColor());
    }
    
    private Stone[][] copyfield(Stone[][] field){
        Stone[][] tempfield = new Stone[field.length][field[1].length];
        for(int z=0;z<field.length;z++){
            System.arraycopy(field[z], 0, tempfield[z], 0, field[z].length);
        }
        return tempfield;
    }
}
